{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ba49c2998ed32020",
   "metadata": {},
   "source": [
    "## Building a Yahoo Finance Agent\n",
    "\n",
    "This tutorial walks you through the process of building a Yahoo Finance Agent using the `yahoo_finance` tool. The agent will be able to retrieve stock data, financial statements, and other financial information from Yahoo Finance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index yfinance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b095b103c429e4cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "from llama_index.agent import OpenAIAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e91dc5fcc2f4cf4",
   "metadata": {},
   "outputs": [],
   "source": [
    "openai.api_key = \"sk-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b0877f51a351135",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "balance_sheet\n",
      "income_statement\n",
      "cash_flow\n",
      "stock_basic_info\n",
      "stock_analyst_recommendations\n",
      "stock_news\n"
     ]
    }
   ],
   "source": [
    "from llama_index.tools.yahoo_finance.base import YahooFinanceToolSpec\n",
    "\n",
    "finance_tool = YahooFinanceToolSpec()\n",
    "\n",
    "finance_tool_list = finance_tool.to_tool_list()\n",
    "for tool in finance_tool_list:\n",
    "    print(tool.metadata.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2d1e7865b0301f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Balance Sheet: \n",
      "                                                         2023-09-30      2022-09-30      2021-09-30      2020-09-30\n",
      "Treasury Shares Number                                          0.0             NaN             NaN             NaN\n",
      "Ordinary Shares Number                                15550061000.0   15943425000.0   16426786000.0   16976763000.0\n",
      "Share Issued                                          15550061000.0   15943425000.0   16426786000.0   16976763000.0\n",
      "Net Debt                                              81123000000.0   96423000000.0   89779000000.0   74420000000.0\n",
      "Total Debt                                           111088000000.0  120069000000.0  124719000000.0  112436000000.0\n",
      "Tangible Book Value                                   62146000000.0   50672000000.0   63090000000.0   65339000000.0\n",
      "Invested Capital                                     173234000000.0  170741000000.0  187809000000.0  177775000000.0\n",
      "Working Capital                                       -1742000000.0  -18577000000.0    9355000000.0   38321000000.0\n",
      "Net Tangible Assets                                   62146000000.0   50672000000.0   63090000000.0   65339000000.0\n",
      "Common Stock Equity                                   62146000000.0   50672000000.0   63090000000.0   65339000000.0\n",
      "Total Capitalization                                 157427000000.0  149631000000.0  172196000000.0  164006000000.0\n",
      "Total Equity Gross Minority Interest                  62146000000.0   50672000000.0   63090000000.0   65339000000.0\n",
      "Stockholders Equity                                   62146000000.0   50672000000.0   63090000000.0   65339000000.0\n",
      "Gains Losses Not Affecting Retained Earnings         -11452000000.0  -11109000000.0     163000000.0    -406000000.0\n",
      "Retained Earnings                                      -214000000.0   -3068000000.0    5562000000.0   14966000000.0\n",
      "Capital Stock                                         73812000000.0   64849000000.0   57365000000.0   50779000000.0\n",
      "Common Stock                                          73812000000.0   64849000000.0   57365000000.0   50779000000.0\n",
      "Total Liabilities Net Minority Interest              290437000000.0  302083000000.0  287912000000.0  258549000000.0\n",
      "Total Non Current Liabilities Net Minority Interest  145129000000.0  148101000000.0  162431000000.0  153157000000.0\n",
      "Other Non Current Liabilities                         34391000000.0   32485000000.0   28636000000.0   26320000000.0\n",
      "Tradeand Other Payables Non Current                   15457000000.0   16657000000.0   24689000000.0   28170000000.0\n",
      "Long Term Debt And Capital Lease Obligation           95281000000.0   98959000000.0  109106000000.0   98667000000.0\n",
      "Long Term Debt                                        95281000000.0   98959000000.0  109106000000.0   98667000000.0\n",
      "Current Liabilities                                  145308000000.0  153982000000.0  125481000000.0  105392000000.0\n",
      "Other Current Liabilities                             58829000000.0   60845000000.0   47493000000.0   42684000000.0\n",
      "Current Deferred Liabilities                           8061000000.0    7912000000.0    7612000000.0    6643000000.0\n",
      "Current Deferred Revenue                               8061000000.0    7912000000.0    7612000000.0    6643000000.0\n",
      "Current Debt And Capital Lease Obligation             15807000000.0   21110000000.0   15613000000.0   13769000000.0\n",
      "Current Debt                                          15807000000.0   21110000000.0   15613000000.0   13769000000.0\n",
      "Other Current Borrowings                               9822000000.0   11128000000.0    9613000000.0    8773000000.0\n",
      "Commercial Paper                                       5985000000.0    9982000000.0    6000000000.0    4996000000.0\n",
      "Payables And Accrued Expenses                         62611000000.0   64115000000.0   54763000000.0   42296000000.0\n",
      "Payables                                              62611000000.0   64115000000.0   54763000000.0   42296000000.0\n",
      "Accounts Payable                                      62611000000.0   64115000000.0   54763000000.0   42296000000.0\n",
      "Total Assets                                         352583000000.0  352755000000.0  351002000000.0  323888000000.0\n",
      "Total Non Current Assets                             209017000000.0  217350000000.0  216166000000.0  180175000000.0\n",
      "Other Non Current Assets                              46906000000.0   39053000000.0   48849000000.0   42522000000.0\n",
      "Non Current Deferred Assets                           17852000000.0   15375000000.0             NaN             NaN\n",
      "Non Current Deferred Taxes Assets                     17852000000.0   15375000000.0             NaN             NaN\n",
      "Investments And Advances                             100544000000.0  120805000000.0  127877000000.0  100887000000.0\n",
      "Other Investments                                               NaN  120805000000.0  127877000000.0  100887000000.0\n",
      "Investmentin Financial Assets                        100544000000.0  120805000000.0  127877000000.0  100887000000.0\n",
      "Available For Sale Securities                        100544000000.0  120805000000.0  127877000000.0  100887000000.0\n",
      "Net PPE                                               43715000000.0   42117000000.0   39440000000.0   36766000000.0\n",
      "Accumulated Depreciation                             -70884000000.0  -72340000000.0  -70283000000.0  -66760000000.0\n",
      "Gross PPE                                            114599000000.0  114457000000.0  109723000000.0  103526000000.0\n",
      "Leases                                                12839000000.0   11271000000.0   11023000000.0   10283000000.0\n",
      "Machinery Furniture Equipment                         78314000000.0   81060000000.0   78659000000.0   75291000000.0\n",
      "Land And Improvements                                 23446000000.0   22126000000.0   20041000000.0   17952000000.0\n",
      "Properties                                                      0.0             0.0             0.0             0.0\n",
      "Current Assets                                       143566000000.0  135405000000.0  134836000000.0  143713000000.0\n",
      "Other Current Assets                                  14695000000.0   21223000000.0   14111000000.0   11264000000.0\n",
      "Inventory                                              6331000000.0    4946000000.0    6580000000.0    4061000000.0\n",
      "Receivables                                           60985000000.0   60932000000.0   51506000000.0   37445000000.0\n",
      "Other Receivables                                     31477000000.0   32748000000.0   25228000000.0   21325000000.0\n",
      "Accounts Receivable                                   29508000000.0   28184000000.0   26278000000.0   16120000000.0\n",
      "Cash Cash Equivalents And Short Term Investments      61555000000.0   48304000000.0   62639000000.0   90943000000.0\n",
      "Other Short Term Investments                          31590000000.0   24658000000.0   27699000000.0   52927000000.0\n",
      "Cash And Cash Equivalents                             29965000000.0   23646000000.0   34940000000.0   38016000000.0\n",
      "Cash Equivalents                                       1606000000.0    5100000000.0   17635000000.0   20243000000.0\n",
      "Cash Financial                                        28359000000.0   18546000000.0   17305000000.0   17773000000.0\n"
     ]
    }
   ],
   "source": [
    "print(finance_tool.balance_sheet(\"AAPL\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bcaa33f006b4fb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = OpenAIAgent.from_tools(finance_tool_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba290fe8f1621839",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The analyst recommendations for AAPL (Apple Inc.) are as follows:\n",
      "\n",
      "- Current period: \n",
      "  - Strong Buy: 11\n",
      "  - Buy: 21\n",
      "  - Hold: 6\n",
      "  - Sell: 0\n",
      "  - Strong Sell: 0\n",
      "\n",
      "- 1 month ago: \n",
      "  - Strong Buy: 10\n",
      "  - Buy: 20\n",
      "  - Hold: 12\n",
      "  - Sell: 1\n",
      "  - Strong Sell: 0\n",
      "\n",
      "- 2 months ago: \n",
      "  - Strong Buy: 10\n",
      "  - Buy: 21\n",
      "  - Hold: 12\n",
      "  - Sell: 1\n",
      "  - Strong Sell: 0\n",
      "\n",
      "- 3 months ago: \n",
      "  - Strong Buy: 10\n",
      "  - Buy: 24\n",
      "  - Hold: 7\n",
      "  - Sell: 1\n",
      "  - Strong Sell: 0\n"
     ]
    }
   ],
   "source": [
    "print(agent.chat(\"What are the analyst recommendations for AAPL?\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
